//引入 koa模块

var Koa = require('koa');

var Router = require('@koa/router');  /*引入是实例化路由** 推荐*/

let cors = require('koa2-cors')

let bodyparser = require('koa-bodyparser')

let range = require('koa-range')

let fs = require('fs')

let path = require('path')

let mine = require('mime-types');

let jsonwebtoken = require("jsonwebtoken");

//实例化
var app = new Koa();

let router = new Router()

app.use(cors())
app.use(bodyparser())
app.use(range)

const songdata = [
    {
        id: 1,
        name: '稻香',
        singer: '周杰伦',
        collect: true,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "0": "稻香 - 周杰伦 (Jay Chou)" },
            { "7730": "词:周杰伦" },
            { "15470": "曲:周杰伦" },
            { "23200": "编曲:黄雨勋" },
            { "30941": "对这个世界如果你有太多的抱怨" },
            { "34222": "跌倒了就不敢继续往前走" },
            { "37310": "为什么人要这么的脆弱堕落" },
            { "41478": "请你打开电视看看" },
            { "43078": "多少人为生命在努力勇敢的走下去" },
            { "47078": "我们是不是该知足" },
            { "49470": "珍惜一切就算没有拥有" },
            { "53998": "还记得你说家是唯一的城堡" },
            { "57680": "随着稻香河流继续奔跑" },
            { "60696": "微微笑 小时候的梦我知道" },
            { "65703": "不要哭让萤火虫带着你逃跑" },
            { "69454": "乡间的歌谣永远的依靠" },
            { "72384": "回家吧 回到最初的美好" },
            { "101086": "不要这么容易就想放弃" },
            { "103306": "就像我说的" },
            { "104796": "追不到的梦想换个梦不就得了" },
            { "107762": "为自己的人生鲜艳上色" },
            { "109820": "先把爱涂上喜欢的颜色" },
            { "112762": "笑一个吧" },
            { "113756": "功成名就不是目的" },
            { "115746": "让自己快乐快乐这才叫做意义" },
            { "118674": "童年的纸飞机" },
            { "120195": "现在终于飞回我手里" },
            { "124418": "所谓的那快乐" },
            { "125810": "赤脚在田里追蜻蜓追到累了" },
            { "128783": "偷摘水果被蜜蜂给叮到怕了" },
            { "131775": "谁在偷笑呢" },
            { "133287": "我靠着稻草人" },
            { "134536": "吹着风 唱着歌 睡着了" },
            { "137512": "午后吉他在虫鸣中更清脆" },
            { "140425": "阳光洒在路上就不怕心碎" },
            { "143036": "珍惜一切 就算没有拥有" },
            { "147653": "还记得你说家是唯一的城堡" },
            { "151435": "随着稻香河流继续奔跑" },
            { "154371": "微微笑 小时候的梦我知道" },
            { "159379": "不要哭让萤火虫带着你逃跑" },
            { "163155": "乡间的歌谣永远的依靠" },
            { "166045": "回家吧 回到最初的美好" },
            { "171083": "还记得你说家是唯一的城堡" },
            { "174819": "随着稻香河流继续奔跑" },
            { "177787": "微微笑 小时候的梦我知道" },
            { "182755": "不要哭让萤火虫带着你逃跑" },
            { "186539": "乡间的歌谣永远的依靠" },
            { "189483": "回家吧 回到最初的美好" }
        ]
    },
    {
        id: 2,
        name: '青花瓷',
        singer: '周杰伦',
        collect: true,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "13000": "素胚勾勒出青花笔锋浓转淡" },
            { "16867": "瓶身描绘的牡丹一如你初妆" },
            { "20583": "冉冉檀香透过窗心事我了然" },
            { "24150": "宣纸上走笔至此搁一半" },
            { "27617": "釉色渲染仕女图韵味被私藏" },
            { "31250": "而你嫣然的一笑如含苞待放" },
            { "35083": "你的美一缕飘散" },
            { "37317": "去到我去不了的地方" },
            { "44033": "天青色等烟雨 而我在等你" },
            { "47800": "炊烟袅袅升起 隔江千万里" },
            { "51433": "在瓶底书汉隶仿前朝的飘逸" },
            { "55233": "就当我为遇见你伏笔" },
            { "59000": "天青色等烟雨 而我在等你" },
            { "62767": "月色被打捞起 晕开了结局" },
            { "66400": "如传世的青花瓷自顾自美丽 你眼带笑意" },
            { "90417": "色白花青的锦鲤跃然於碗底" },
            { "94183": "临摹宋体落款时却惦记著你" },
            { "98017": "你隐藏在窑烧里千年的秘密" },
            { "101650": "极细腻犹如绣花针落地" },
            { "106000": "帘外芭蕉 惹骤雨门环惹铜绿" },
            { "109817": "而我路过那江南小镇惹了你" },
            { "113750": "在泼墨山水画里" },
            { "116083": "你从墨色深处被隐去" },
            { "122700": "天青色等烟雨 而我在等你" },
            { "126467": "炊烟袅袅升起 隔江千万里" },
            { "130000": "在瓶底书汉隶仿前朝的飘逸" },
            { "133800": "就当我为遇见你伏笔" },
            { "137567": "天青色等烟雨 而我在等你" },
            { "141333": "月色被打捞起 晕开了结局" },
            { "145067": "如传世的青花瓷自顾自美丽 你眼带笑意" },
            { "154083": "天青色等烟雨 而我在等你" },
            { "157850": "炊烟袅袅升起 隔江千万里" },
            { "161483": "在瓶底书汉隶仿前朝的飘逸" },
            { "165283": "就当我为遇见你伏笔" },
            { "169050": "天青色等烟雨 而我在等你" },
            { "172817": "月色被打捞起 晕开了结局" },
            { "176550": "如传世的青花瓷自顾自美丽 你眼带笑意" }
        ]

    },
    {
        id: 3,
        name: '富士山下',
        singer: '陈奕迅',
        collect: false,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "1480": "富士山下 - 陈奕迅" },
            { "6060": "作词:林夕" },
            { "23450": "拦路雨偏似雪花 饮泣的你冻吗" },
            { "27690": "这风褛我给你磨到有襟花" },
            { "32280": "连掉了渍也不怕 怎么始终牵挂" },
            { "36870": "苦心选中今天想车你回家" },
            { "41450": "原谅我不再送花 伤口应要结疤" },
            { "45890": "花瓣铺满心里坟场才害怕" },
            { "50540": "如若你非我不嫁 彼此终必火化" },
            { "55120": "一生一世等一天需要代价" },
            { "59660": "谁都只得那双手 靠拥抱亦难任你拥有" },
            { "64750": "要拥有必先懂失去怎接受" },
            { "69390": "曾沿着雪路浪游 为何为好事泪流" },
            { "73980": "谁能凭爱意要富士山私有" },
            { "78550": "何不把悲哀感觉 假设是来自你虚构" },
            { "83030": "试管里找不到它染污眼眸" },
            { "87720": "前尘硬化像石头 随缘地抛下便逃走" },
            { "95390": "我绝不罕有 往街里绕过一周 我便化乌有" },
            { "104010": "情人节不要说穿 只敢抚你发端" },
            { "108450": "这种姿态可会令你更心酸" },
            { "112990": "留在汽车里取暖 应该怎么规劝" },
            { "117630": "怎么可以将手腕忍痛划损" },
            { "122270": "人活到几岁算短 失恋只有更短" },
            { "126750": "归家需要几里路谁能预算" },
            { "131340": "忘掉我跟你恩怨 樱花开了几转" },
            { "135890": "东京之旅一早比一世遥远" },
            { "140480": "谁都只得那双手 靠拥抱亦难任你拥有" },
            { "145570": "要拥有必先懂失去怎接受" },
            { "150210": "曾沿着雪路浪游 为何为好事泪流" },
            { "154750": "谁能凭爱意要富士山私有" },
            { "159390": "何不把悲哀感觉 假设是来自你虚构" },
            { "163980": "试管里找不到它染污眼眸" },
            { "168470": "前尘硬化像石头 随缘地抛下便逃走" },
            { "175730": "我绝不罕有 往街里绕过一周 我便化乌有" },
            { "184700": "谁都只得那双手 靠拥抱亦难任你拥有" },
            { "189800": "要拥有必先懂失去怎接受" },
            { "194740": "曾沿着雪路浪游 为何为好事泪流" },
            { "199430": "谁能凭爱意要富士山私有" },
            { "203930": "何不把悲哀感觉 假设是来自你虚构" },
            { "208470": "试管里找不到它染污眼眸" },
            { "212960": "前尘硬化像石头 随缘地抛下便逃走" },
            { "220220": "我绝不罕有 往街里绕过一周 我便化乌有" },
            { "231570": "你还嫌不够 我把这陈年风褛 送赠你解咒" }
        ]

    },
    {
        id: 4,
        name: '如果爱忘了',
        singer: '周杰伦',
        collect: true,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "0": "总有一些话 来不及说了" },
            { "5000": "总有一个人 是心口的朱砂" },
            { "9000": "想起那些话 那些傻 眼泪落下" },
            { "13000": "只留一句 你现在好吗" },
            { "17000": "如果爱忘了 泪不想落下" },
            { "21000": "那些幸福啊 让她替我到达" },
            { "25000": "如果爱懂了" },
            { "27000": "承诺的代价" },
            { "29000": "不能给我的" },
            { "31000": "请完整给她" },
            { "33000": "总有些牵挂 旧的像伤疤" },
            { "37000": "越是不碰它 越隐隐的痛在那" },
            { "41000": "想你的脸颊 你的发 我不害怕" },
            { "45000": "就让时间 给我们回答" },
            { "49000": "如果爱忘了 泪不想落下" },
            { "53000": "那些幸福啊 让她替我到达" },
            { "57000": "如果爱懂了" },
            { "59000": "承诺的代价" },
            { "61000": "不能给我的" },
            { "63000": "请完整给她" },
            { "65000": "我说我忘了 不痛了" },
            { "69000": "那是因为太爱太懂了" },
            { "73000": "笑了 原谅了 为你也值得" },
            { "77000": "用你的快乐 告诉我" },
            { "81000": "现在放开双手是对的" },
            { "85000": "别管我多 舍不得" },
            { "89000": "如果爱忘了 就放他走吧" },
            { "93000": "那些幸福啊 让她替我到达" },
            { "97000": "如果爱懂了" },
            { "99000": "承诺的代价" },
            { "101000": "不能给我的" },
            { "103000": "请完整给她" },
            { "105000": "如果爱忘了" },
            { "109000": "你还记得吗" }
        ]
    },
    {
        id: 5,
        name: '擦肩而过',
        singer: '胡雯',
        collect: false,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "0": "女:爱上你是我的错" },
            { "3710": "可是离开又舍不得" },
            { "7180": "听着你 为我写的歌" },
            { "10570": "好 难过" },
            { "12280": "" },
            { "14050": "如果说我说如果" },
            { "17750": "我们还能重新来过" },
            { "21200": "不去计较 谁对谁错" },
            { "24360": "该怎么做" },
            { "26260": "" },
            { "28390": "男:我从来没想过" },
            { "31630": "会有这样的结果" },
            { "35600": "成为了彼此 的过客" },
            { "40650": "" },
            { "42570": "是爱的太少" },
            { "45490": "还是要求的太多" },
            { "49570": "总是 会觉得 不快乐" },
            { "54000": "" },
            { "56540": "曾经就算是一个吻" },
            { "59400": "我们也能从寂寞中挣 脱" },
            { "63540": "就算用泪水 换来笑容 也值得" },
            { "68310": "" },
            { "70480": "而现在就算拥抱着" },
            { "73330": "为何两颗心总不知所 措" },
            { "77420": "难道注定就这样 擦肩 而 过" },
            { "82770": "" },
            { "84320": "当爱情来了谁又能说我舍 不得寂寞" },
            { "91260": "当爱情走了谁又能说我不 觉得失落" },
            { "98250": "当爱燃烧过谁又能说我不 觉得快乐" },
            { "105160": "当爱变沉默谁又能说我过得 很洒脱" },
            { "111000": "女:爱上你是我的错" },
            { "115070": "可是离开又舍不得" },
            { "118520": "听着你 为我写的歌" },
            { "121810": "好 难过" },
            { "123700": "" },
            { "125260": "如果说我说如果" },
            { "129010": "我们还能重新来过" },
            { "132530": "不去计较 谁对谁错" },
            { "135630": "该怎么做" },
            { "138070": "" },
            { "139930": "当爱情来了谁又能说我舍 不得寂寞" },
            { "146940": "当爱情走了谁又能说我不 觉得失落" },
            { "153850": "当爱燃烧过谁又能说我不 觉得快乐" },
            { "160830": "当爱变沉默谁又能说我过得 很洒脱" },
            { "167710": "" },
            { "169560": "当爱情来了谁又能说我舍 不得寂寞" },
            { "176490": "当爱情走了谁又能说我不 觉得失落" },
            { "183420": "当爱燃烧过谁又能说我不 觉得快乐" },
            { "190420": "当爱变沉默谁又能说我过得 很洒脱" }
        ]

    },
    {
        id: 6,
        name: '我们的爱',
        singer: '飞儿乐团',
        collect: true,
        weight: 0,
        img: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        album: 'https://imgessl.kugou.com/stdmusic/480/20250828/20250828171322733905.jpg',
        comment: [
            {
                id: 1,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 2,
                comments: '哈哈哦啊好哦'
            },
            {
                id: 3,
                comments: '哈哈哦啊好哦'
            },
        ],
        lyrics: [
            { "0": "我们的爱" },
            { "16140": "回忆里想起模糊的小时候" },
            { "28980": "云朵漂浮在蓝蓝的天空" },
            { "34490": "那时候的你说" },
            { "38840": "要和我手牵手" },
            { "42580": "一起走到时间的尽头" },
            { "49650": "从此以后我都不敢抬头看" },
            { "56910": "彷佛我的天空失去了颜色" },
            { "62690": "从那一天起" },
            { "66870": "我忘记了呼吸" },
            { "70740": "眼泪啊永远不再" },
            { "74260": "不再哭泣" },
            { "79930": "我们的爱" },
            { "83870": "过了就不再回来" },
            { "87770": "直到现在" },
            { "89890": "我还默默的等待" },
            { "96180": "我明白" },
            { "98330": "已变成你的负担" },
            { "101880": "只是永远" },
            { "103980": "我都放不开" },
            { "107040": "最后的温暖啊～～" },
            { "116280": "你给的温暖" },
            { "121850": "从此以后我都不敢抬头看" },
            { "129110": "彷佛我的天空失去了颜色" },
            { "134890": "从那一天起" },
            { "139070": "我忘记了呼吸" },
            { "142940": "眼泪啊永远不再" },
            { "146460": "不再哭泣" },
            { "152130": "我们的爱" },
            { "156070": "过了就不再回来" },
            { "159970": "直到现在" },
            { "162090": "我还默默的等待" },
            { "168380": "我明白" },
            { "170530": "已变成你的负担" },
            { "174080": "只是永远" },
            { "176180": "我都放不开" },
            { "179240": "最后的温暖啊～～" },
            { "188480": "你给的温暖" },
            { "194150": "不要再问你是否爱我" },
            { "197830": "现在我想要自由的天空" },
            { "201350": "远离开这被捆绑的世界" },
            { "204890": "不再寂寞" },
            { "205330": "我们的爱" },
            { "209270": "过了就不再回来" },
            { "213170": "直到现在" },
            { "215290": "我还默默的等待" },
            { "221580": "我明白" },
            { "223730": "已变成你的负担" },
            { "227280": "只是永远" },
            { "229380": "我都放不开" },
            { "232440": "最后的温暖啊～～" },
            { "241680": "你给的温暖" }
        ]

    }
]

const loginDate = [
    {
        id: 1,
        user: 'admin1',
        password: 'admin111',
        phone: '18524166257'
    },
    {
        id: 2,
        user: 'admin2',
        password: 'admin222',
        phone: '18524166258'
    },
    {
        id: 3,
        user: 'admin3',
        password: 'admin333',
        phone: '18524166259'
    },
]


// 获取歌单所有数据
router.get('/api/song', async (ctx) => {
    ctx.body = {
        code: 200,
        data: songdata
    }
})

// 获取用户登录信息
router.get('/api/logindata', async (ctx) => {
    ctx.body = {
        code: 200,
        data: loginDate
    }
})

// 密码登录
router.post('/api/mimalogin', async (ctx) => {
    try {
        let { password, user } = ctx.request.body
        let t = loginDate.find(item => item.password == password && item.user == user)
        console.log(t)
        if (t) {
            let token = jsonwebtoken.sign({ user }, 'your-256-bit-secret', { expiresIn: '1d' })
            ctx.body = {
                code: 200,
                message: '登录成功',
                token
            }
        } else {
            ctx.body = {
                code: 404,
                message: '未找到登录信息'
            }
        }
    } catch (error) {
        console.log('登录方法错误' + error);

    }
})

// 手机号登录
router.post('/api/phonelogin', async (ctx) => {
    try {
        let { phone } = ctx.request.body
        let t = loginDate.find(item => item.phone == phone)
        if (t) {
            let token = jsonwebtoken.sign({ phone }, 'your-256-bit-secret', { expiresIn: '1d' })
            ctx.body = {
                code: 200,
                message: '登录成功',
                token
            }
        } else {
            ctx.body = {
                code: 404,
                message: '未找到登录信息'
            }
        }
    } catch (error) {
        console.log('登录方法错误' + error);

    }
})


// 注册
router.post('/api/register', async (ctx) => {
    try {
        let { phone, password } = ctx.request.body
        if (phone == '' || password == '') {
            ctx.body = {
                code: 500,
                message: '账号密码不能为空'
            }
            return
        }
        let t = loginDate.find(item => item.password == password && item.phone == phone)
        if (t) {
            ctx.body = {
                code: 500,
                message: '该用户已注册'
            }
        } else {
            let obj = {
                id: new Date().getTime(),
                phone,
                password,
                user: 'user' + loginDate.length + 1
            }
            loginDate.push(obj)
            ctx.body = {
                code: 200,
                message: '注册成功'
            }
        }
    } catch (error) {
        console.log('注册失败' + error);

    }
})

// 查询歌曲单个信息
router.get('/api/getsong/:id', async (ctx) => {
    const id = ctx.params.id
    const searchsong = songdata.filter(item => item.id == id)
    ctx.body = {
        code: 200,
        data: searchsong
    }
})

// 切换歌曲
router.get('/api/switchsong/:id', async (ctx) => {
    try {
        const id = ctx.params.id
        const audiourl = path.join(__dirname, 'audio', id + '.m4a')

        if (!fs.existsSync(audiourl)) {
            ctx.body = {
                code: 404,
                message: '文件不存在'
            }
            return
        }

        // 3. 获取文件信息（大小、MIME类型）
        const stats = await fs.promises.stat(audiourl); // 获取文件状态（含大小）
        const mimeType = mine.lookup(audiourl); // 自动识别MIME类型（如audio/mp4）
        const fileSize = stats.size; // 文件总大小（用于断点续传）
        // 4. 配置核心响应头（关键！）
        ctx.set({
            'Content-Type': mimeType || 'audio/mp4', // 优先自动识别，默认.m4a的MIME类型
            'Content-Length': fileSize, // 告诉浏览器文件总大小
            'Accept-Ranges': 'bytes', // 声明支持断点续传（配合koa-range）
            'Content-Disposition': 'inline', // 指定“内联播放”，而非下载
            'Cache-Control': 'public, max-age=31536000', // 缓存优化
        });

        ctx.body = fs.createReadStream(audiourl)
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '服务器内部错误'
        }
    }

})

// 收藏歌曲
router.post('/api/collectsong', async (ctx) => {
    try {
        // 接收参数名为id
        const id = ctx.request.body.id
        songdata.map(item => {
            if (item.id == id) {
                item.collect = !item.collect
            }
            return item
        })
        ctx.body = {
            code: 200,
            message: '收藏成功'
        }
    } catch (error) {
        console.log('收藏方法错误' + error);

    }
})

app.use(router.routes());   /*启动路由*/
app.use(router.allowedMethods());

app.listen(3000);